import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ValidationPipe } from "@nestjs/common";
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
import { DateFormatInterceptor } from "./common/interceptors/date-format.interceptor";
import { ConfigService } from "@nestjs/config";
import * as bcrypt from "bcrypt";
import { NestExpressApplication } from "@nestjs/platform-express";
import * as path from "path";

async function bootstrap() {
	const app = await NestFactory.create<NestExpressApplication>(AppModule);

	// 设置全局前缀
	app.setGlobalPrefix("api");

	// 使用全局验证管道
	app.useGlobalPipes(new ValidationPipe());

	// 配置Swagger文档
	const config = new DocumentBuilder()
		.setTitle("Points API")
		.setDescription("Points API 文档")
		.setVersion("1.0")
		.addBearerAuth()
		.build();
	const document = SwaggerModule.createDocument(app, config);
	SwaggerModule.setup("api", app, document);

	// 全局拦截器
	app.useGlobalInterceptors(new DateFormatInterceptor());

	// 启用CORS
	app.enableCors();

	// 配置静态资源服务
	app.useStaticAssets(path.join(__dirname, "..", "uploads"), {
		prefix: "/uploads/",
	});

	const configService = app.get(ConfigService);
	// 使用端口3001，避免3000端口冲突
	const port = 3000; // 修改为3001

	await app.listen(port);
	console.log(`应用已启动: http://localhost:${port}`);
}
bootstrap();
